package cn.colins.raft.rpc.enums;


import cn.colins.raft.exception.RaftException;
import cn.colins.raft.exception.RaftRunException;

public enum MsgTypeEnum {
    CLIENT_REQUEST((short) 0xFBFF, "客户端请求"),
    CLIENT_REQUEST_RESULT((short) 0xFBF0, "客户端请求响应"),

    LOG_PULL_REQUEST((short) 0xABFF, "日志拉取请求"),
    LOG_PULL_REQUEST_RESULT((short) 0xABF0, "日志拉取请求请求响应"),

    LOG_PRE_COMMIT((short) 0xBBFF, "Log预提交"),
    LOG_PRE_COMMIT_RESULT((short) 0xBBF0, "Log预提交返回"),
    LOG_COMMIT((short) 0xBBB0, "Log提交"),

    HEART_BEAT((short) 0xFFFF, "心跳"),
    HEART_BEAT_RESULT((short) 0xFFF0, "心跳返回"),

    VOTE_RPC((short) 0xFFF1, "请求投票"),
    VOTE_RPC_RESULT((short) 0xF1F1, "请求投票返回");



    private final short code;
    private final String desc;

    MsgTypeEnum(short code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public short getCode() {
        return code;
    }

    public static MsgTypeEnum getInstance(short code){
        for(MsgTypeEnum msgTypeEnum:MsgTypeEnum.values()){
            if(msgTypeEnum.code==code){
                return msgTypeEnum;
            }
        }
        throw new RaftRunException("无对应请求类型");
    }
}
